package com.kaiwukj.android.ufamily.mvp.ui.page.mine.evaluate;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerOrderComponent;
import com.kaiwukj.android.ufamily.di.module.OrderModule;
import com.kaiwukj.android.ufamily.mvp.helper.GlideEngine;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.OrderEvaluateParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.HouseKeeperResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderDetailResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderResult;
import com.kaiwukj.android.ufamily.mvp.ui.adapter.ImageAdapter;
import com.kaiwukj.android.ufamily.mvp.ui.page.order.OrderContract;
import com.kaiwukj.android.ufamily.mvp.ui.page.order.OrderPresenter;
import com.kaiwukj.android.ufamily.mvp.ui.widget.home.PopupWindowUtil;
import com.kaiwukj.android.ufamily.mvp.ui.widget.order.CustomRatingBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.OrderEvaluateEditUrl)
public class OrderEvaluateEditActivity extends BaseMvpActivity<OrderPresenter> implements OrderContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_service_name)
    TextView tvServiceName;
    @BindView(R.id.tv_service_price)
    TextView tvServicePrice;
    @BindView(R.id.et_evaluate)
    EditText etEvaluate;
    @BindView(R.id.rv_imgs)
    RecyclerView rvImgs;
    @BindView(R.id.rating_bar_01)
    CustomRatingBar mRatingBar01;
    @BindView(R.id.rating_bar_02)
    CustomRatingBar mRatingBar02;
    @BindView(R.id.rating_bar_03)
    CustomRatingBar mRatingBar03;

    @Autowired
    int orderId;
    @Autowired
    String avatarUrl;
    @Autowired
    String serviceName;
    @Autowired
    String servicePrice;

    private ImageAdapter mImageAdapter;
    private int serviceQuality = 5;
    private int serviceAttitude = 5;
    private int workEfficiency = 5;
    private int serviceRating = 5;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @OnClick(R.id.btn_submit)
    void onSubmit() {

        OrderEvaluateParams params = new OrderEvaluateParams();
        params.setOrderId(orderId);
        params.setServiceQuality(serviceQuality);
        params.setServiceAttitude(serviceAttitude);
        params.setWorkEfficiency(workEfficiency);
        params.setServiceRating(((float) (serviceAttitude + serviceQuality + workEfficiency) / 3));
        String content = etEvaluate.getText().toString().trim();
        if (!StringUtils.isEmpty(content)) {
            params.setComments(content);
        } else {
            showMessage("请输入您的服务评价吧~");
            return;
        }
        mPresenter.submitEvaluate(params, mImageAdapter.getData());
        showLoading("正在提交...");
    }


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerOrderComponent.builder()
                .appComponent(appComponent)
                .orderModule(new OrderModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_order_evaluate_edit;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("发表评价");
        rvImgs.setLayoutManager(new GridLayoutManager(this, 4));
        mImageAdapter = new ImageAdapter();
        mImageAdapter.setMaxCount(3);
        rvImgs.setAdapter(mImageAdapter);
        mRatingBar01.setOnRatingChangeListener(ratingCount ->
                serviceQuality = (int) ratingCount);
        mRatingBar02.setOnRatingChangeListener(ratingCount ->
                serviceAttitude = (int) ratingCount);
        mRatingBar03.setOnRatingChangeListener(ratingCount ->
                workEfficiency = (int) ratingCount);
        mImageAdapter.setOnItemClickListener(new ImageAdapter.onItemClickListener() {
            @Override
            public void onAddClick() {
                pickImage();
            }

            @Override
            public void onDelClick(int position) {
                mImageAdapter.remove(position);
            }

            @Override
            public void onImgClick(List<String> imgUrls, int position) {
                PopupWindowUtil.createImageDialog(OrderEvaluateEditActivity.this,
                        imgUrls.get(position)).showCenter(R.layout.activity_order_evaluate_edit);
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        Glide.with(this)
                .load(avatarUrl)
                .into(ivAvatar);
        tvServiceName.setText(serviceName);
        tvServicePrice.setText(String.format("¥ %s", servicePrice));
    }

    private void pickImage() {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .isCompress(true)
                .maxSelectNum(3 - mImageAdapter.getData().size())
                .imageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.MyImagePickerStyle)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> result) {
                        List<String> path = new ArrayList<>();
                        for (LocalMedia media : result) {
                            path.add(media.getCompressPath());
                        }
                        mImageAdapter.addData(path);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    @Override
    public void onFetchOrder(List<OrderResult> results) {

    }

    @Override
    public void onGetOrder(OrderDetailResult result) {

    }

    @Override
    public void onOrderPayResult(Map<String, Object> result) {

    }

    @Override
    public void onGetHouseKeeper(HouseKeeperResult result) {

    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (taskId == OrderPresenter.EVALUATE) {
            hideLoading();
            showMessage("提交成功");
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        hideLoading();
    }
}
